<?php
namespace jy_storeping;
defined('BY_JYA') or exit('error');
/*
	门店代理
*/
class area extends \plugin{
	public function get_area_code($area){
		$address=$area['province'];
		$oldaddress=$area['province'];
		if($area['city']){
			$address.=' '.$area['city'];
			$oldaddress.=' 直辖市';
		}
		if($area['district']){
			$address.=' '.$area['district'];
			$oldaddress.=' '.$area['district'];
		}
		if($area['street']){
			$address.=' '.$area['street'];
			$oldaddress.=' '.$area['street'];
		}
		$area_code=pdo_getcolumn('core_area',array('address'=>$address),'code');
		if(!$area_code){
			$isup=pdo_count('core_area',array(),'code');
			if($isup==0){
				require ROOT_D.'/update/update_area.php';
				$area_code=pdo_getcolumn('core_area',array('address'=>$address),'code');
			}else{
				$area_code=pdo_getcolumn('core_area',array('address'=>$oldaddress),'code');
				if($area_code){
					pdo_query('DROP TABLE IF EXISTS '.TABLE_PRE.'core_area;');
					require ROOT_D.'/update/update_area.php';
				}
			}
		}
		return $area_code;
	}
	/*
		申请成为区域代理
	*/
	public function apply_agent($mid,$area_code,$post=array()){
		if(!$area_code && !$post['is_duodai']){
			exi('获取区域code失败！','error');
		}
		$check=$this->getcolumn('area_store',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid,'area_code'=>$area_code),'id');
		if($check){
			exi('您已经代理了该地区，请勿重复代理！','error');
		}
		if($post['area_type']!=4){
			$check=$this->getcolumn('area_store',array('area_type'=>$post['area_type'],'uniacid'=>$_SESSION['uniacid'],'area_code'=>$area_code),'id');
			if($check){
				exi('该区域已存在代理！请更换！','error');
			}
		}
		$new_apply=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$mid,
			'status'=>0,
			'area_type'=>$post['area_type'],
			'area_code'=>$area_code,
			'area'=>ijson_encode($post['area']),
			'storeid'=>$post['storeid'],
			'realname'=>$post['realname'],
			'mobile'=>$post['mobile'],
			'wechat'=>$post['wechat'],
			'title'=>$post['title'],
			'createtime'=>TIMESTAMP,
		);
		$this->insert('area_store',$new_apply);
		$id=$this->insertid();
		return $id;
	}
	public function up_agent($mid,$area_code,$post=array()){
		if(!$area_code && !$post['is_duodai']){
			//exi('请选择区域省份！','error');
		}
		$new_apply=array();
		if($area_code){
			$new_apply['area_code']	=$area_code;
			$new_apply['area_type']	=pdo_getcolumn('core_area',array('code'=>$area_code),'type');
		}
		$this->update('area_store',$new_apply,array('uniacid'=>$_SESSION['uniacid'],'id'=>$post['id']));
		return $id;
	}
	/*
		审核区域代理
	*/
	public function check_agent($agent_id,$status,$remark=''){
		if(!$agent_id || !$status){
			return;
		}
		$area_store=$this->get('area_store',array('id'=>$agent_id));
		if(!$area_store){
			return;
		}
		$update=array(
			'status'=>$status,
			'check_time'=>TIMESTAMP,
		);
		if($update['status']==1){
		}else{
			$update['remark']=$remark;
		}
		if(!$area_store['storeid']){
			$storeid=$this->upstore($area_store);
			$update['storeid']=$storeid;
		}
		$this->update('area_store',$update,array('id'=>$agent_id));
		return true;
	}
	public function upstore($area_store){
		if($area_store['storeid']){
			return $area_store['storeid'];
		}
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$area_store['mid'],
			'pcd'=>$area_store['area'],
			'realname'=>$post['realname'],
			'mobile'=>$post['mobile'],
			'title'=>$area_store['title'],
		);
		pdo_insert('jy_weishop_store',$data);
		$storeid=pdo_insertid();
		return $storeid;
	}
	public function set_parent($mid,$fromuser){
		$this->update('area_store',array('agent_id'=>$fromuser),array('mid'=>$mid));
		return true;
	}
	/*
		禁用代理商
	*/
	public function stop_agent($agent_id){
		$this->update('area_store',array('status'=>2),array('mid'=>$agent_id));
	}
	/*
		恢复代理商
	*/
	public function start_agent($agent_id){
		$this->update('area_store',array('status'=>1),array('mid'=>$agent_id));
	}
	/*
		分润
	*/
	public function add_fee($order=array()){
		if(!$order['storeid']){
			return false;
		}
		$this->isfee=0;
		//当前店
		$area_store=$this->get('area_store',array('storeid'=>$order['storeid']));
		$feeinfo=$this->get_commission($order);
		$pcd=json_decode($area_store['area'],true);
		if($area_store['area_type']==4){
			$money=$feeinfo[4];
			$this->add_commission($order,$area_store['storeid'],4,$money);
		}
		//区代理
		if($area_store['area_type']>=3){
			if($area_store['area_type']!=3){
				$area_code=pdo_getcolumn('core_area',array('address'=>$pcd['province'].' '.$pcd['city'].' '.$pcd['district']),'code');
				$area3=$this->get('area_store',array('uniacid'=>$_SESSION['uniacid'],'area_type'=>3,'area_code'=>$area_code));
			}else{
				$area3=$area_store;
			}
			$money=$feeinfo[3]-$this->isfee;
			$this->add_commission($order,$area3['storeid'],3,$money);
		}
		//市代理
		if($area_store['area_type']>=2){
			if($area_store['area_type']!=2){
				$area_code=pdo_getcolumn('core_area',array('address'=>$pcd['province'].' '.$pcd['city']),'code');
				$area2=$this->get('area_store',array('uniacid'=>$_SESSION['uniacid'],'area_type'=>2,'area_code'=>$area_code));
			}else{
				$area2=$area_store;
			}
			$money=$feeinfo[2]-$this->isfee;
			$this->add_commission($order,$area2['storeid'],2,$money);
		}
		//省代理
		if($area_store['area_type']>=1){
			if($area_store['area_type']!=1){
				$area_code=pdo_getcolumn('core_area',array('address'=>$pcd['province']),'code');
				$area1=$this->get('area_store',array('uniacid'=>$_SESSION['uniacid'],'area_type'=>1,'area_code'=>$area_code));
			}else{
				$area1=$area_store;
			}
			$money=$feeinfo[1]-$this->isfee;
			$this->add_commission($order,$area1['storeid'],1,$money);
		}
		return true;
	}
	/*
		获取门店佣金
	*/
	public function get_commission($order=array()){
		$areasetting=$this->plugin_setting('areasetting');
		$feerate1=$areasetting['fee_1'];
		$feerate2=$areasetting['fee_2'];
		$feerate3=$areasetting['fee_3'];
		$feerate4=$areasetting['fee_4'];
		$order_price=$order['price'];
		$money1=0;
		$money2=0;
		$money3=0;
		$money4=0;
		foreach($order['goods'] as $goods){
			$ignore=$this->get('fee_goods',array('goods_id'=>$goods['goodsid'],'uniacid'=>$_SESSION['uniacid']));
			$goods_commission=$ignore['commissions'];
			$goods_commission=$goods_commission?json_decode($goods_commission,true):array();
			if($ignore['ignore'] || !$ignore['id']){
					if($ignore['ignore']){
						$order_price=$order_price-$goods['total_price'];
					}else{
						$this->needorder=true;
					}
					continue;
			}else{
				$total_price=$goods['total_price'];
				$goodsfeerat1=$goods_commission['commission1'];
				$goodsfeerat2=$goods_commission['commission2'];
				$goodsfeerat3=$goods_commission['commission3'];
				$goodsfeerat4=$goods_commission['commission4'];
				if(substr($goodsfeerate1,-1)=='%'){
					$goods_commission_money=($total_price*substr($goodsfeerate1,0,-1))/100;
					$money1+=$goods_commission_money;
				}else{
					$goods_commission_money=$goodsfeerate1*$goods['total'];
					$money1+=$goods_commission_money;
				}
				if(substr($goodsfeerate2,-1)=='%'){
					$goods_commission_money=($total_price*substr($goodsfeerate2,0,-1))/100;
					$money2+=$goods_commission_money;
				}else{
					$goods_commission_money=$goodsfeerate2*$goods['total'];
					$money2+=$goods_commission_money;
				}
				if(substr($goodsfeerate3,-1)=='%'){
					$goods_commission_money=($total_price*substr($goodsfeerate3,0,-1))/100;
					$money3+=$goods_commission_money;
				}else{
					$goods_commission_money=$goodsfeerate3*$goods['total'];
					$money3+=$goods_commission_money;
				}
				if(substr($goodsfeerate4,-1)=='%'){
					$goods_commission_money=($total_price*substr($goodsfeerate4,0,-1))/100;
					$money4+=$goods_commission_money;
				}else{
					$goods_commission_money=$goodsfeerate4*$goods['total'];
					$money4+=$goods_commission_money;
				}
				$order_price=$order_price-$total_price;
			}
		}
		if($this->needorder && $order_price>0){
			if(substr($feerate1,-1)=='%'){
				$commission_money=($order_price*substr($feerate1,0,-1))/100;
				$money1+=$commission_money;
			}elseif($feerate1>0){
				$commission_money=$feerate1;
				$money1+=$commission_money;
			}
			if(substr($feerate2,-1)=='%'){
				$commission_money=($order_price*substr($feerate2,0,-1))/100;
				$money2+=$commission_money;
			}elseif($feerate2>0){
				$commission_money=$feerate2;
				$money2+=$commission_money;
			}
			if(substr($feerate3,-1)=='%'){
				$commission_money=($order_price*substr($feerate3,0,-1))/100;
				$money3+=$commission_money;
			}elseif($feerate3>0){
				$commission_money=$feerate3;
				$money3+=$commission_money;
			}
			if(substr($feerate4,-1)=='%'){
				$commission_money=($order_price*substr($feerate4,0,-1))/100;
				$money4+=$commission_money;
			}elseif($feerate4>0){
				$commission_money=$feerate4;
				$money4+=$commission_money;
			}
			
		}
		$allfee=array(
			1=>$money1,
			2=>$money2,
			3=>$money3,
			4=>$money4,
		);
		return $allfee;
	}
	/*
		门店佣金
	*/
	public function add_commission($order,$storeid=0,$area_type=0,$money=0){
		if(!$storeid || !$area_type){
			return false;
		}
		if($money>0){
			$this->isfee +=$money;
			$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'storeid'=>$storeid,
						'mid'=>$order['mid'],
						'ordersn'=>$order['ordersn'],
						'money'=>$money,
						'total_price'=>$order['price'],
						'remark'=>'区域门店代理',
						'createtime'=>TIMESTAMP,
						'type'=>$area_type+3,
						'status'=>0,
			);
			pdo_insert('jy_weishop_store_commission',$new_commission);
		}
		return true;
	}
}